package com.mycar2w.cloud.service.serviceimpl;

import com.mycar2w.cloud.entity.ApplieOrder;
import com.mycar2w.cloud.entity.Car;
import com.mycar2w.cloud.entity.TUser;
import com.mycar2w.cloud.service.repository.ApplieOrderRepository;
import com.mycar2w.cloud.service.repository.CarRepository;
import com.mycar2w.cloud.service.repository.TUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * created time 2017/4/4
 *
 * @author ljc
 * @version V1.0
 */
@Service
public class CommService {
    @Autowired
    private CarRepository carRepository;
    @Autowired
    private TUserRepository tUserRepository;
    @Autowired
    private ApplieOrderRepository applieOrderRepository;

    /**
     * 首页将要显示的汽车的列表 每页显示12辆
     * @param pageIndex 页码
     * @return 汽车列表 List
     */
    public List<Car> findCarListByNone(Integer pageIndex) {
        Page<Car> pageCars = carRepository.getPageByNone(pageIndex);
        List<Car> listCars=pageCars.getContent();
        return listCars;
    }

    /**
     * 查询在某个价格区间的汽车列表
     * @param beginPrice 其实价格
     * @param endPrice 最终价格
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车列表 List
     */
    public List<Car> findCarListByBetweenPrice(String beginPrice,String endPrice,Integer pageIndex,Integer sort){
        Page<Car> pageCars=carRepository.getPageByBetweenPriceNo(beginPrice,endPrice,pageIndex,sort);
        List<Car> listCars=pageCars.getContent();
        return listCars;
    }

    /**
     * 查询某个地区的汽车列表
     * @param area 地址
     * @param pageIndex 页码
     * @return 汽车列表 List
     */
    public List<Car> findCarListByArea(Long area, Integer pageIndex,Integer sort) {
        List<TUser> tUsers= tUserRepository.findByAddressId(area);
        List<Long> tuserIdList=tUsers.stream().map(tUser-> tUser.getId()).collect(Collectors.toList());
        Page<Car> pageCars=carRepository.getPageByArea(tuserIdList,pageIndex,sort);
        List<Car> listCars=pageCars.getContent();
        return listCars;
    }

    /**
     * 查询交易次数的汽车列表
     * @param count 交易次数
     * @param pageIndex 页码
     * @param sort 排序规则
     * @return 汽车列表 List
     */
    public List<Car> findCarListByTransfercount(String count, Integer pageIndex, Integer sort) {
        Page<Car> pageCars=carRepository.getPageByTransferCount(count,pageIndex,sort);
        List<Car> listCars=pageCars.getContent();
        return listCars;
    }

    public List<Car> findCarListByName(String carName, Integer pageIndex) {
        Page<ApplieOrder> orders= applieOrderRepository.getFromLikeCarName(carName,pageIndex);
//        List<ApplieOrder> orders=applieOrderRepository.findByCarXinghaoLike(carName);
        List<ApplieOrder> orderList=orders.getContent();
        List<Car> listCars=carRepository.getPageFromApplieOrders(orderList);
        return listCars;
    }
}
